/**
 * http://usejsdoc.org/
 */
var dns = require('dns'),    //dns查询
    querystring = require('querystring'); //字符串处理

/**
 * 获取dns信息
 * @param postData form数据
 * @param callback 回调内容
 */
function getDns(postData,callback){
	var domain = querystring.parse(postData).search_dns;
	/**异步解析域名*/
	dns.resolve(domain,function(err,addresses){
		if(!addresses){
			addresses = ['不存在域名'];
		}
		callback(domain,addresses);
	});
}

/**
 * 解析DNS
 * @param req
 * @param res
 */
exports.parseDns = function(req, res){
	var postData = "";
	req.addListener("data",function (postDataChunk){
		postData += postDataChunk;
	});
	req.addListener("end",function (){
		var retData = getDns(postData,function(domain,addresses){
			res.writeHead( 200 , {"Content-Type":"text/html"});
			res.end("<html>" +
					"<head>" +
						"<meta http-equiv='content-type'  content='text/html;charset=utf-8' />" +
					"</head>" +
					"<div style='text-align: center;'>" +
						"Domain:<span style='color:red;'>"+domain+"</span><br/>" +
						"IP:<span style='color:red;'>"+addresses.join(',')+"</span>" +
					"</div>" +
					"</html>");
		});
		return;
	});
};